import { AppStateStatus } from "react-native"
import { router } from "expo-router"
import { Instance, SnapshotIn, SnapshotOut, types } from "mobx-state-tree"

import { withSetPropAction } from "./helpers/withSetPropAction"

/**
 * Model description here for TypeScript hints.
 */
export const SystemModel = types
  .model("System")
  .props({
    isShowVerifyModel: types.maybe(types.boolean),
    androidCameraClickPermission: types.optional(types.boolean, false),
    vivoFirstShowCameraPermission: types.optional(types.boolean, false),
    showWatermark: types.optional(types.boolean, true),
    androidStyleCardClickPermission: types.optional(types.boolean, false),
    androidAllowPolicy: types.optional(types.boolean, false),
    showWelcome: types.optional(types.boolean, true),
    secondRequestNoticePermissionTimes: types.optional(types.number, 0),
  })
  .volatile(() => ({
    showLoadingModal: false,
    selectedDate: new Date(), // 将 selectedDate 设置为 volatile，保证不会持久化
    appState: "inactive",
    pushToken: types.optional(types.string, ""),
  }))
  .actions(withSetPropAction)
  .views((self) => ({
    get selectedDateFormat() {
      const year = self.selectedDate.getFullYear()
      const month = String(self.selectedDate.getMonth() + 1).padStart(2, "0") // 月份从0开始
      const day = String(self.selectedDate.getDate()).padStart(2, "0")
      return `${year}-${month}-${day}` // 格式化为 'YYYY-MM-DD'
    },
  })) // eslint-disable-line @typescript-eslint/no-unused-vars
  .actions((self) => ({
    async updateShowVerifyModel(isShow: boolean) {
      self.isShowVerifyModel = isShow
    },
    async showModal(params: { message?: string; maxDuration?: number }) {
      const query = new URLSearchParams({
        message: params.message || "",
        maxDuration: String(params.maxDuration ?? 0),
      }).toString()
      router.push(`/appLoading?${query}`)
      self.showLoadingModal = true
    },
    async hideModal() {
      self.showLoadingModal = false
    },
    async updateDate(date: Date) {
      self.selectedDate = date
    },
    async updateAppState(state: AppStateStatus) {
      self.appState = state
    },
    async setShowWatermark(value: boolean) {
      self.showWatermark = value
    },
    androidAllowCameraPermission() {
      self.setProp("androidCameraClickPermission", true)
    },
    vivoAllowCameraPermission() {
      self.setProp("vivoFirstShowCameraPermission", true)
    },
    removeAndroidAllowCameraPermission() {
      self.setProp("androidCameraClickPermission", false)
    },
    androidAllowStyleCardPermission() {
      self.setProp("androidStyleCardClickPermission", true)
    },
    removeAndroidAllowStyleCardPermission() {
      self.setProp("androidStyleCardClickPermission", false)
    },
    androidClickAllowPolicy() {
      self.setProp("androidAllowPolicy", true)
    },
    removeAndroidAllowPolicy() {
      self.setProp("androidAllowPolicy", false)
    },
    setShowWelcome(value: boolean) {
      self.showWelcome = value
    },
    setPushToken(value: string | any) {
      self.pushToken = value
    },
    setSecondRequestNoticePermission() {
      self.secondRequestNoticePermissionTimes = self.secondRequestNoticePermissionTimes + 1
    },
  })) // eslint-disable-line @typescript-eslint/no-unused-vars

export interface System extends Instance<typeof SystemModel> {}

export interface SystemSnapshotOut extends SnapshotOut<typeof SystemModel> {}

export interface SystemSnapshotIn extends SnapshotIn<typeof SystemModel> {}

export const createSystemDefaultModel = () => types.optional(SystemModel, {})
